package ExerChapter13Wrapper;

import java.util.Scanner;

public class StringBuffer_ {
    public static void main(String[] args) {
        System.out.println("请输入手机名称和价格！");
        Scanner sc = new Scanner(System.in);

        String foo = sc.nextLine();

        try {
            String[] foos = foo.split(" ");
            try {
                // ·需要两个反斜杠来实现正则
                Double.parseDouble(foos[1]);
                if (foos[1].contains(".")){
                    String[] foos1 = foos[1].split("\\.");
                    StringBuffer str0 = new StringBuffer(foos1[0]);
                    // 用str0.lastIndexOf(".")也行
                    for (int i = str0.length() - 3; i > 0; i-=3) {
                        str0.insert(i,",");
                    }
                    System.out.println(foos[0] + " " + str0.append("." + foos1[1]).toString());
                } else{
                    StringBuffer str0 = new StringBuffer(foos[1]);
                    for (int i = str0.length() - 3; i > 0; i-=3) {
                        str0.insert(i,",");
                    }
                    System.out.println(foos[0] + " " + str0.toString());
                }

            } catch (NumberFormatException e) {
                System.out.println("价格必须是数字！");
            } 

        } catch (IndexOutOfBoundsException e) {
            System.out.println("需要输入一个name和一个数字");
        }

        // System.out.println(String.format("%s的价格是%s", str1.toString(),str2.toString()));
        
    }
    
}
